﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Threading;
using System.Net;
using System.IO;
namespace ServiceOfSansar
{
    class Program
    {

        static ServiceHost[] host = new ServiceHost[5];
        static DateTime prevRunDate;
        static void Main(string[] args)
        {
            try
            {
                host[0] = new ServiceHost(typeof(Master));
                host[1] = new ServiceHost(typeof(Register));
                host[2] = new ServiceHost(typeof(Enquiry));
                host[3] = new ServiceHost(typeof(Transfer));
                host[4] = new ServiceHost(typeof(Report));
                

                host[0].Open();
                host[1].Open();
                host[2].Open();
                host[3].Open();
                host[4].Open();

                foreach (var server in host)
                    foreach (var item in server.Description.Endpoints)
                    {
                        Console.WriteLine("> [{0}] {1}", item.Binding.Name, item.Address.Uri);
                    }
                prevRunDate = DateTime.Now;
                System.Threading.Timer timer1;
                System.Threading.Timer timer2;
                System.Threading.Timer timer3;

                timer1 = new System.Threading.Timer(new TimerCallback(DoSomething), null, 0, 1000 * 60 * 30);
                timer2 = new System.Threading.Timer(new TimerCallback(ftpGetFile), null, 1000, 1000 * 60 * 30);
                timer3 = new System.Threading.Timer(new TimerCallback(ftpSetFile), null, 0, 1000 * 60 * 30);
                //timer3 = new System.Threading.Timer(new TimerCallback(asdf), null, 1000, 1000*60*30);

                //ftpFileList();
                Console.WriteLine("Enter !");
                Console.ReadKey();                
                host[0].Close();
                host[1].Close();
                host[2].Close();
                host[3].Close();
                host[4].Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Exception:"+ex.Message);
                Console.ReadKey();
            }
         
        }
  
        private static void ftpSetFile(object obj)
        {
            if (DateTime.Now.Hour == 0)
            {
                ftpInterface ftp = new ftpInterface();
                ftp.CreateBillFileAndSend(1);
            }
        }

        private static void ftpGetFile(object obj)
        {
            //Console.WriteLine("ftpGetFile:" + DateTime.Now.ToString());

            ftpInterface ftp = new ftpInterface();
            ftp.ftpReadAndTransaction();
        }

        private static void DoSomething(object obj)
        {
            //it executes every second
            //Console.WriteLine("DoSomething:" + DateTime.Now.ToString());

            if (DateTime.Now.Hour == 1 && prevRunDate.ToString("yyyyMMdd") != DateTime.Now.ToString("yyyyMMdd"))
            {
                new Transfer().DoUtmTxns(DateTime.Now);
                prevRunDate = DateTime.Now;
            }
             
        }
    }
}
